<template>
  <el-card class="point">
    <el-container>
      <el-header style="height:30px;">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ name: 'point' }">积分</el-breadcrumb-item>
          <el-breadcrumb-item>积分列表</el-breadcrumb-item>
        </el-breadcrumb>
        <el-button type="success" class="add-btn" size="mini" @click="showDialog(0)" plain>添加积分</el-button>
      </el-header>
      <el-main>
        <!-- 表格 -->
        <el-table :data="tableData" style="width: 100%">
          <el-table-column prop="id" label="积分id" min-width="100"> </el-table-column>
          <el-table-column prop="userId" label="用户id" min-width="70"> </el-table-column>
          <el-table-column prop="userName" label="用户名" min-width="70">
          </el-table-column>
          <el-table-column prop="email" label="邮箱" min-width="80">
          </el-table-column>
          <el-table-column prop="points" label="分数" min-width="50">
          </el-table-column>
          <el-table-column prop="createTime" label="创建时间" min-width="100">
            <template v-slot="{ row }">
              {{ row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column prop="updateTime" label="修改时间" min-width="100">
            <template v-slot="{ row }">
              {{ row.createTime | formatDate }}
            </template>
          </el-table-column>
          <el-table-column min-width="100">
            <template slot="header" slot-scope="scope">
              <el-input v-model="keyword" size="mini" placeholder="输入id搜索" :clearable="true"
                @clear="clearIpt(fetchData, scope)">
                <el-button slot="append" icon="el-icon-search" @click="searchData"> </el-button>
              </el-input>
            </template>
            <template slot-scope="{ row }">
              <el-link type="primary" :underline="false" @click="showDialog(1, row)">
                修改积分
              </el-link>
              <el-link type="danger" :underline="false" @click="deleteById($api.deletePoint, fetchData, row.id, '积分')">
                删除积分
              </el-link>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
      <el-pagination @size-change="handleSizeChange($event, fetchData)"
        @current-change="handleCurrentChange($event, fetchData)" :page-sizes="[1, 2, 5, 10]"
        layout="total,sizes, prev, pager, next" :total="total">
      </el-pagination>
    </el-container>
    <!-- 弹窗 -->
    <!-- dialog -->
    <el-dialog :title="['添加积分', '修改积分'][this.form.flag]" :visible.sync="dialogFormVisible" width="30%"
      @close="resetForm('form')">
      <el-form :model="form" size="small" ref="form" :rules="rules">
        <el-form-item label="分数" prop="points">
          <el-input v-model="form.points" autocomplete="off" maxlength="20" show-word-limit> </el-input>
        </el-form-item>
        <el-form-item label="用户id" prop="userId">
          <el-input v-model="form.userId" autocomplete="off" maxlength="20" show-word-limit> </el-input>
        </el-form-item>
        <el-form-item label="用户名字" prop="userName">
          <el-input v-model="form.userName" autocomplete="off" maxlength="20" show-word-limit> </el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="form.email" autocomplete="off" maxlength="20" show-word-limit> </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="submitDialog('form')" size="small">
          {{ ['添加', '修改'][this.form.flag] }}
        </el-button>
      </div>
    </el-dialog>
  </el-card>
</template>
<script>
import mixins from '@mixins'
import { ADD, EDIT } from '@static'
import { deepClone } from '@utils'
export default {
  data() {
    return {
      tableData: [], // 表格数据
      total: undefined, // 总数
      dialogFormVisible: false, // 控制弹窗显示
      keyword: '' // 搜索关键字
    }
  },
  mixins: [mixins.admin],
  created() {
    this.fetchData()
  },
  methods: {
    async fetchData() {
      const res = await this.$api.getPointPage(this.query)
      console.log(res);
      this.tableData = res.list
      this.total = res.total
    },
    showDialog(flag, data) {
      this.dialogFormVisible = true
      if (flag === ADD) {
        this.form = {}
        this.form.flag = flag
      } else if (flag === EDIT) {
        this.form = deepClone(data)
        this.form.flag = flag
      }
    },
    async submitDialog(formName) {
      this[formName].updateTime = Date.now()
      if (this[formName].flag === ADD) {
        this[formName].createTime = Date.now()
        const { success } = await this.$api.addPoint(this[formName])
        this.handleSuccess(success, '添加积分', this.fetchData)
      } else if (this[formName].flag === EDIT) {
        const { success } = await this.$api.editPoint(this[formName])
        this.handleSuccess(success, '修改积分', this.fetchData)
      }
      this.dialogFormVisible = false
    },
    async searchData() {
      const res = await this.$api.searchPoint({ id: this.keyword })
      const saveData = [];
      saveData.push(res.data)
      this.tableData = saveData
    }
  }
}
</script>
<style>
@import '~@css/aCommon.less';
</style>
